// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TypeDescriptorProvider.SafeEnum2.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Globalization;
    using System.IO;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    using Org.OpenTrader.Framework.LiveObjects;

    #endregion

    /// <summary>
    /// The enum type description provider.
    /// </summary>
    public class EnumConfigurationFieldTypeDescriptionProvider : System.ComponentModel.TypeDescriptionProvider
    {
        #region Public Methods

        /// <summary>
        /// The get type descriptor.
        /// </summary>
        /// <param name="objectType">
        /// The object type.
        /// </param>
        /// <param name="instance">
        /// The instance.
        /// </param>
        /// <returns>
        /// </returns>
        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
        {
            return new TypeDescriptor(objectType);
        }

        #endregion

        /// <summary>
        /// The converter.
        /// </summary>
        internal class Converter : StringConverter
        {
            #region Public Methods

            /// <summary>
            /// The convert to.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <param name="culture">
            /// The culture.
            /// </param>
            /// <param name="value">
            /// The value.
            /// </param>
            /// <param name="destinationType">
            /// The destination type.
            /// </param>
            /// <returns>
            /// The convert to.
            /// </returns>
            public override object ConvertTo(
                ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                if (value is ConfigurationField.Enum)
                {
                    return (value as ConfigurationField.Enum).Contents;
                }

                return value;
            }

            #endregion
        }

        /// <summary>
        /// The editor.
        /// </summary>
        internal class Editor : UITypeEditor
        {
            // Displays the UI for value selection.
            #region Public Methods

            /// <summary>
            /// The edit value.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <param name="provider">
            /// The provider.
            /// </param>
            /// <param name="value">
            /// The value.
            /// </param>
            /// <returns>
            /// The edit value.
            /// </returns>
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                var editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (editorService != null)
                {
                    var editor = new UIEnumConfigurationFieldEditor(value as ConfigurationField.Enum, editorService);
                    editorService.DropDownControl(editor);
                }

                return value;
            }

            /// <summary>
            /// The get edit style.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <returns>
            /// </returns>
            public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }

            #endregion
        }

        /// <summary>
        /// The type descriptor.
        /// </summary>
        internal class TypeDescriptor : CustomTypeDescriptor
        {
            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="TypeDescriptor"/> class.
            /// </summary>
            /// <param name="type">
            /// The type.
            /// </param>
            public TypeDescriptor(Type type)
            {
            }

            #endregion

            #region Public Methods

            /// <summary>
            /// The get editor.
            /// </summary>
            /// <param name="editorBaseType">
            /// The editor base type.
            /// </param>
            /// <returns>
            /// The get editor.
            /// </returns>
            public override object GetEditor(Type editorBaseType)
            {
                return new Editor();
            }

            #endregion
        }
    }
}